package com.sinochem.sales.auth.util;

import com.sinochem.sales.common.constant.CommonConstant;
import com.sinochem.sales.common.constant.SecurityConstants;
import com.sinochem.sales.admin.api.to.UserTO;
import com.sinochem.sales.admin.api.vo.MenuVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xuan😴🐶🐱🐽🌺  <a href="zhangshixuanj@163.com">联系作者</a>
 * @version Revision:v1.0,Date: 2017/11/12 9:59
 * @project parent
 * @description security
 * @Modification Date: 2017/11/12 9:59  {填写修改说明}
 */
public class UserDetailsImpl implements UserDetails {
    private static final long serialVersionUID = 1L;

    private Integer userId;
    private String username;
    private String password;
    private String status;
    private List<MenuVO> menuList;


    public UserDetailsImpl(UserTO userTO) {
        this.userId = userTO.getUserId();
        this.username = userTO.getUsername();
        this.password = userTO.getPassword();
        this.status = userTO.getDelFlag();
        menuList = userTO.getMenuList();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //authorityList.add(new SimpleGrantedAuthority(SecurityConstants.BASE_ROLE));
        return menuList.stream()
                .filter(menuVO -> !StringUtils.isEmpty(menuVO.getPath()))
                .map(menuVO -> new SimpleGrantedAuthority(menuVO.getPath()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    // 账户是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    // 账户是否未锁定
    @Override
    public boolean isAccountNonLocked() {
        return !StringUtils.equals(CommonConstant.STATUS_LOCK, status);
    }

    // 密码是否未过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    // 账户是否激活
    @Override
    public boolean isEnabled() {
        return StringUtils.equals(CommonConstant.STATUS_NORMAL, status);
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<MenuVO> getMenuList() {
        return menuList;
    }

    public void setMenuList(List<MenuVO> menuList) {
        this.menuList = menuList;
    }
}
